import BenderConfig from "../../sdk/config";

const ChatHouseClient = {
  state: {
    userRelation: {
      chatHouseUserList: '', // 群列表
    },
    groupChangeState: 0,
    currentMsgs: [],
    loginUser: {}
  },

  mutations: {// 设置 state属性
    removeFromGroup (state, payload) {//人员退群后更新本地聊天室缓存
      const {data} = payload
      const groupMap = state.userRelation.chatHouseUserList
      const groups = groupMap.get(data.group + '')
      // eslint-disable-next-line no-array-constructor
      const tempList = new Array()
      groups.users.forEach(user => {
        if (user.userId !== data.user.userId) {
          tempList.push(user)
        }
      })
      const groupDto = {
        groupId: groups.groupId + '',
        name: groups.name,
        users: tempList
      }
      state.userRelation.chatHouseUserList = {}
      groupMap.set(data.group + '', groupDto)
      state.userRelation.chatHouseUserList = groupMap
    },
    updateGroupAndFriendList (state, payload) { // 更新群组和好友列表 暂时只有群组
      const {data} = payload
      state.loginUser = {
        userId: data.userId,
        avatar: data.avatar,
        terminal: data.terminal,
        nick: data.nick
      }
      var groups = data.groups
      if (groups) {
        const groupMap = new Map()
        data.groups.forEach(group => {
          groupMap.set(group.groupId + '', group)
        })
        state.userRelation.chatHouseUserList = groupMap
      }
    },
    updateMsg (state, payload) { // 更新群组和好友列表 暂时只有群组
      const {data} = payload
      if (data) {
        const groupList = state.userRelation.chatHouseUserList.get('100')
        const groupUserList = groupList.users
        let fromUser = {}
        if (groupUserList.length > 0) {
          // eslint-disable-next-line
          groupUserList.forEach(group => {
            groupUserList.forEach(user => {
              if (user.userId === data.from) {
                if (!user.bgImg) {
                  user.bgImg = 'https://cdn.quasar.dev/img/mountains.jpg'
                }
                fromUser = user
                return
              }
            })
          })
        }
        data.user = fromUser
        if (data.msgType !== 0) {
          let file = JSON.parse(data.content)
          file.url = BenderConfig.filePath + file.url
          data.content = file
        }
        const dateMap = new Map()// 用来记录发送的月日 排除只显示一次
        let chatId = ''
        if (data.groupId) {
          chatId = 'groupMsg' + localStorage.getItem('chatGroupId')
        } else {
          chatId = localStorage.getItem('chatHisId')
        }
        const historyMsg = localStorage.getItem(chatId)
        // eslint-disable-next-line no-array-constructor
        let hisList = new Array()
        if (historyMsg) {
          hisList = JSON.parse(historyMsg)
        }
        hisList.push(data)
        if (hisList.length >= 50) {
          hisList = hisList.slice(hisList.length - 20, hisList.length)
        }
        hisList.forEach(res => {
          if (dateMap.get(res.sentDay)) { // 排除每一条记录都显示日子
            res.sentDay = ''
          } else {
            dateMap.set(res.sentDay, '1')
          }
        })
        localStorage.setItem(chatId, JSON.stringify(hisList))
        const temp = state.groupChangeState + 1
        state.groupChangeState = temp
      }
    },
    changerChatUser (state) {
      const temp = state.groupChangeState + 1
      state.groupChangeState = temp
    }
  },
  actions: {// 方法
  },
  getters: {
    onGetLoginInfo (state) {
      console.log('userLoginInfo-----', state.loginUser)
      return state.loginUser
    },
    onGetMsg (state) {
      // eslint-disable-next-line no-unused-vars
      let temp = state.groupChangeState
      let chatId = ''
      let historyMsg = ''
      const chatHisId = localStorage.getItem('chatHisId')
      if (chatHisId) {
        chatId = chatHisId
      } else {
        chatId = 'groupMsg' + localStorage.getItem('chatGroupId')
      }
      historyMsg = localStorage.getItem(chatId)
      if (historyMsg && chatId) {
        return JSON.parse(historyMsg)
      } else {
        return []
      }
    },
    onGetChatHouseUserList (state) {
      const groupId = localStorage.getItem('chatGroupId')
      if (state.userRelation.chatHouseUserList && groupId) {
        return state.userRelation.chatHouseUserList.get(groupId)
      } else {
        return ''
      }
    }
  }
}
export default ChatHouseClient
